package com.example.project.dao;


import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.example.project.XMLParser;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class SQLiteComer extends SQLiteOpenHelper {
	
	static final String KEY_RESTAURANT = "restaurant"; 
 	static final String KEY_ID = "id";
 	static final String KEY_NAME = "nom";
 	static final String KEY_DIRECTION = "direccio";
 	static final String KEY_TELEFON = "telefon";
 	static final String KEY_THUMB_URL = "thumb_url";
 	static final String KEY_IMG="img";
 	static final String KEY_VALORACIO="valoracio";
 	static final String KEY_LATITUD="latitud";
 	static final String KEY_LONGITUD="longitud";
 	static final String KEY_DIST="distancia";
 	static final String URL = "https://sites.google.com/site/balaguerrcardsp/menjar/menjar.xml";
 	//static final String URL= "http://www.rcardsp.tk/serviceguia.php";
 	static final String TABLE_NAME = "comer";
 	public static String DB_PATH = "/data/data/YOUR_PACKAGE/databases/";
    public SQLiteComer(Context context, String name, CursorFactory factory, int version) {
		super(context, name, factory, version);
	}
	
   // String sqlCreate = "CREATE TABLE comer ( idComer integer primary key autoincrement,idFoto integer, idDireccion integer, idTipo integer, nombre TEXT, telefono TEXT, descripcion TEXT)";
    String sqlCreate = "CREATE TABLE comer (" +
    		KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "+
    		KEY_IMG + " TEXT, "+
    		KEY_DIRECTION +  " TEXT, "+
    		KEY_LATITUD + " TEXT, "+
    		KEY_LONGITUD + " TEXT, "+
    		KEY_NAME + " TEXT, " +
    		KEY_TELEFON + " TEXT);";

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(sqlCreate);
        this.init(db);
    }
 
    @Override
    public void onUpgrade(SQLiteDatabase db, int versionAnterior, int versionNueva) {
        db.execSQL("DROP TABLE IF EXISTS comer");
        db.execSQL(sqlCreate);
    }
    
    private void init(SQLiteDatabase db){
    	XMLParser parser = new XMLParser();
		String xml = parser.getXmlFromUrl(URL); 
		Document doc = parser.getDomElement(xml); 
		NodeList nl = doc.getElementsByTagName(KEY_RESTAURANT);
		ContentValues values = new ContentValues(); 
    	
		for (int i = 0; i < nl.getLength(); i++) {
			Element e = (Element) nl.item(i);
			values.put(KEY_ID, parser.getValue(e, KEY_ID));
			values.put(KEY_NAME, parser.getValue(e, KEY_NAME));
			values.put(KEY_DIRECTION, parser.getValue(e, KEY_DIRECTION));
			values.put(KEY_TELEFON, parser.getValue(e, KEY_TELEFON));
			//values.put(KEY_VALORACIO, parser.getValue(e, KEY_VALORACIO));
			values.put(KEY_LONGITUD, parser.getValue(e, KEY_LONGITUD));
			values.put(KEY_LATITUD, parser.getValue(e, KEY_LATITUD));
			values.put(KEY_IMG, parser.getValue(e, KEY_THUMB_URL));

			// adding HashList to ArrayList
			db.insert(SQLiteComer.TABLE_NAME, null, values);
		}
    }
    
	 public void changeDir (String myPath){
		 SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
		 
	 }
		
    

}
